package main

import (
	"im-mq/db"
	"im-mq/mq"
	"im-mq/user"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

var (
	WebApp *gin.Engine
)

func getCurrentDirectory() string {
	dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	return strings.Replace(dir, "\\", "/", -1)
}

func main() {
	// chdir
	os.Chdir(getCurrentDirectory())
	// db
	db.Init_DB()
	// web
	WebApp = gin.Default()
	WebApp.Use(cors.Default())

	mq.Init_Once(WebApp)
	user.Init_Once(WebApp)

	// static
	WebApp.Static("/static", "./static")
	WebApp.StaticFile("/favicon.ico", "./static/favicon.png")
	WebApp.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusSeeOther, "/static")
	})
	// run
	WebApp.Run(":9000")
}
